import { ApiCall } from "tsrpc";
import { ReqGetDataByTime, ResGetDataByTime } from "../../shared/protocols/pandian/PtlGetDataByTime";
import { Global } from "../../models/Global";

// This is a demo code file
// Feel free to delete it

export default async function (call: ApiCall<ReqGetDataByTime, ResGetDataByTime>) {

    const { startTime, endTime } = call.req.time;
    // Error
    const ret = await Global.db.collection('pandian').find({
        time: {
          $gte: new Date(startTime),
          $lte: new Date(endTime) 
        }
      }).toArray();

    if (!ret) {
        call.error('记录不存在');
        return;
    }
    call.succ({
        data: ret.map(item => ({
            _id: item._id.toString(),
            SerialNum: item.SerialNum,
            AssetNum: item.AssetNum,
            EquipmentName: item.EquipmentName,
            EquipmentModel: item.EquipmentModel,
            AbnormalInfo: item.AbnormalInfo,
            User: item.User,
            time: item.time,
            Location: item.Location
        }))
    })
}  